<template>
  <div>
    <el-dialog :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="30%" :before-close="handleClose"
      :close-on-click-modal="false">
      <div class="content">
        <div class="title">
          <svg-icon class="svg-icon" icon-class="infoAdd" />
          <span>扣杂标准</span>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="125px" class="demo-ruleForm">
          <el-form-item label="农场" prop="farmCode">
            <el-select style="width: 100%;" v-model="ruleForm.farmCode" filterable placeholder="请选择农场或输入关键词"
              @change="handleGetTeam">
              <el-option v-for="item in farmList" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="生产队" prop="teamCode">
            <el-select style="width: 100%;" v-model="ruleForm.teamCode" filterable placeholder="请选择生产队或输入关键词">
              <el-option v-for="item in teamList" :key="item.value" :label="item.label" :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="作物类型" prop="cropName">
            <el-select style="width: 100%;" v-model="ruleForm.cropName" filterable placeholder="请选择作物类型或输入关键词">
              <el-option v-for="item in cropList" :key="item.value" :label="item.label" :value="item.label">
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="扣杂百分比(%)" prop="standard">
            <el-input v-model="ruleForm.standard" placeholder="请输入扣杂百分比(%)"></el-input>
          </el-form-item>
          <el-form-item label="填报人" prop="createUser">
            <el-input v-model="ruleForm.createUser" placeholder="请输入填报人" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="填报时间" prop="createTime">
            <el-date-picker style="width: 100%;" v-model="ruleForm.createTime" format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择日期时间" :disabled="true">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div class="dialog-footer-info">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import rules from '@/utils/rules'
import { getUserKey } from '@/utils/auth'
import { formatDate } from '@/utils/methods'
import { deductStandardAddApi, deductStandardEditApi } from '@/api/info'
import { cropinfoSelectApi, selectApi } from '@/api/select'
export default {
  props: ['showDialog'],
  data() {
    return {
      ruleForm: {
        farmCode: '',
        teamCode: '',
        cropName: '',
        standard: '',
        createUser: '',
        createTime: ''
      },
      rules: rules,
      cropList: [],
      farmList: [],
      teamList: [],
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        farmCode: '',
        teamCode: '',
        cropName: '',
        standard: '',
        createUser: '',
        createTime: ''
      }
      this.$refs.ruleForm.resetFields();
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            let { code } = await deductStandardEditApi(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            }
          } else {
            let { code } = await deductStandardAddApi(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
            }
          }
          this.$emit('handleRefresh')
          this.handleClose()
        } else {
          return false;
        }
      });
    },
    // 获取填报人时间
    handleGetInfo() {
      this.ruleForm.createUser = getUserKey() || 'nkjtcyf'
      this.ruleForm.createTime = formatDate(new Date())
    },
    // 获取作物类型
    async handleGetSelect() {
      let res = await cropinfoSelectApi()
      this.cropList = res.data
    },
    // 获取农场
    async handleGetFarm() {
      let res = await selectApi({ type: 'farm_type' })
      this.farmList = res.data.dictItemList
      if (this.$status != '0') {
        this.farmList = res.data.dictItemList.filter(item => item.value == this.$farmCode)
        this.handleGetTeam(this.$farmCode)
        this.ruleForm.farmCode = this.$farmCode
      }
    },
    // 获取生产队
    async handleGetTeam(e) {
      let res = await selectApi({ type: e })
      this.teamList = res.data.dictItemList
      if (this.$status == '2') {
        this.ruleForm.teamCode = this.$teamCode
        this.teamList = res.data.dictItemList.filter(item => item.value == this.$teamCode)
      }
    },
    // 农场生产队回填
    async handleGetDetail(row) {
      let farm = await selectApi({ type: 'farm_type' })
      this.farmList = farm.data.dictItemList.filter(item => item.value == row.farmCode)
      let team = await selectApi({ type: row.farmCode })
      this.teamList = team.data.dictItemList
      this.ruleForm = { ...row }
    },
  }
}
</script>

<style lang='scss' scoped></style>
